# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
import shutil


class Recipe(recipe.Recipe):
    name = 'm4'
    version = '1.4.18'
    licenses = [License.GPLv2Plus]
    stype = SourceType.TARBALL
    url = 'gnu://'
    tarball_checksum = 'f2c1e86ca0a404ff281631bdc8377638992744b175afb806e25871a24a934e07'
    files_bins = ['m4']

    patches = ['m4/0001-Fix-build-invalid-instruction-on-macOS-10.13.patch',
               'm4/0001-m4-compilation-with-glibc-2.28.patch']

    def prepare(self):
        self.configure_options += " --disable-gcc-warnings"
        self.append_env('CFLAGS', '-Wno-error=cast-align')

    def post_install(self):
        # HACK m4 build fails in Windows gmp build:
        # m4  -DHAVE_CONFIG_H -D__GMP_WITHIN_GMP -DOPERATION_invert_limb_table -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 -DDLL_EXPORT -DPIC invert_limb_table.asm >tmp-invert_limb_table.s
        # m4.exe: write error: Invalid argument
        if self.config.platform == Platform.WINDOWS:
            shutil.move(os.path.join(self.config.prefix, 'bin', 'm4.exe'),
                        os.path.join(self.config.prefix, 'bin', 'm4.exe.backup'))
 
